using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace Microsoft.Phone.Controls.Primitives
{
	public class RelativeAnimatingContentControl : ContentControl
	{
		private enum DoubleAnimationDimension
		{
			Width,
			Height
		}
		private abstract class AnimationValueAdapter
		{
			public RelativeAnimatingContentControl.DoubleAnimationDimension Dimension
			{
				get;
				private set;
			}
			public AnimationValueAdapter(RelativeAnimatingContentControl.DoubleAnimationDimension dimension)
			{
				this.Dimension = dimension;
			}
			public abstract void UpdateWithNewDimension(double width, double height);
		}
		private abstract class GeneralAnimationValueAdapter<T> : RelativeAnimatingContentControl.AnimationValueAdapter
		{
			private double _ratio;
			protected T Instance
			{
				get;
				set;
			}
			protected double InitialValue
			{
				get;
				private set;
			}
			protected abstract double GetValue();
			protected abstract void SetValue(double newValue);
			public GeneralAnimationValueAdapter(RelativeAnimatingContentControl.DoubleAnimationDimension d, T instance) : base(d)
			{
				this.Instance = instance;
				this.InitialValue = this.StripMagicNumberOff(this.GetValue());
				this._ratio = this.InitialValue / 100.0;
			}
			public double StripMagicNumberOff(double number)
			{
				if (base.Dimension != RelativeAnimatingContentControl.DoubleAnimationDimension.Width)
				{
					return number - 0.2;
				}
				return number - 0.1;
			}
			public static RelativeAnimatingContentControl.DoubleAnimationDimension? GetDimensionFromMagicNumber(double number)
			{
				double num = Math.Round(number);
				double num2 = Math.Abs(number - num);
				if (num2 >= 0.09999100000000001 && num2 <= 0.100009)
				{
					return new RelativeAnimatingContentControl.DoubleAnimationDimension?(RelativeAnimatingContentControl.DoubleAnimationDimension.Width);
				}
				if (num2 >= 0.199991 && num2 <= 0.20000900000000002)
				{
					return new RelativeAnimatingContentControl.DoubleAnimationDimension?(RelativeAnimatingContentControl.DoubleAnimationDimension.Height);
				}
				return default(RelativeAnimatingContentControl.DoubleAnimationDimension?);
			}
			public override void UpdateWithNewDimension(double width, double height)
			{
				double sizeToUse = (base.Dimension == RelativeAnimatingContentControl.DoubleAnimationDimension.Width) ? width : height;
				this.UpdateValue(sizeToUse);
			}
			private void UpdateValue(double sizeToUse)
			{
				this.SetValue(sizeToUse * this._ratio);
			}
		}
		private class DoubleAnimationToAdapter : RelativeAnimatingContentControl.GeneralAnimationValueAdapter<DoubleAnimation>
		{
			protected override double GetValue()
			{
				return base.Instance.get_To().get_Value();
			}
			protected override void SetValue(double newValue)
			{
				base.Instance.set_To(new double?(newValue));
			}
			public DoubleAnimationToAdapter(RelativeAnimatingContentControl.DoubleAnimationDimension dimension, DoubleAnimation instance) : base(dimension, instance)
			{
			}
		}
		private class DoubleAnimationFromAdapter : RelativeAnimatingContentControl.GeneralAnimationValueAdapter<DoubleAnimation>
		{
			protected override double GetValue()
			{
				return base.Instance.get_From().get_Value();
			}
			protected override void SetValue(double newValue)
			{
				base.Instance.set_From(new double?(newValue));
			}
			public DoubleAnimationFromAdapter(RelativeAnimatingContentControl.DoubleAnimationDimension dimension, DoubleAnimation instance) : base(dimension, instance)
			{
			}
		}
		private class DoubleAnimationFrameAdapter : RelativeAnimatingContentControl.GeneralAnimationValueAdapter<DoubleKeyFrame>
		{
			protected override double GetValue()
			{
				return base.Instance.get_Value();
			}
			protected override void SetValue(double newValue)
			{
				base.Instance.set_Value(newValue);
			}
			public DoubleAnimationFrameAdapter(RelativeAnimatingContentControl.DoubleAnimationDimension dimension, DoubleKeyFrame frame) : base(dimension, frame)
			{
			}
		}
		private const double SimpleDoubleComparisonEpsilon = 9E-06;
		private double _knownWidth;
		private double _knownHeight;
		private List<RelativeAnimatingContentControl.AnimationValueAdapter> _specialAnimations;
		public RelativeAnimatingContentControl()
		{
			base.add_SizeChanged(new SizeChangedEventHandler(this.OnSizeChanged));
		}
		private void OnSizeChanged(object sender, SizeChangedEventArgs e)
		{
			if (e != null && e.get_NewSize().get_Height() > 0.0 && e.get_NewSize().get_Width() > 0.0)
			{
				this._knownWidth = e.get_NewSize().get_Width();
				this._knownHeight = e.get_NewSize().get_Height();
				RectangleGeometry rectangleGeometry = new RectangleGeometry();
				rectangleGeometry.set_Rect(new Rect(0.0, 0.0, this._knownWidth, this._knownHeight));
				base.set_Clip(rectangleGeometry);
				this.UpdateAnyAnimationValues();
			}
		}
		private void UpdateAnyAnimationValues()
		{
			if (this._knownHeight > 0.0 && this._knownWidth > 0.0)
			{
				if (this._specialAnimations == null)
				{
					this._specialAnimations = new List<RelativeAnimatingContentControl.AnimationValueAdapter>();
					IEnumerator enumerator = VisualStateManager.GetVisualStateGroups(this).GetEnumerator();
					try
					{
						while (enumerator.MoveNext())
						{
							VisualStateGroup visualStateGroup = (VisualStateGroup)enumerator.get_Current();
							if (visualStateGroup != null)
							{
								IEnumerator enumerator2 = visualStateGroup.get_States().GetEnumerator();
								try
								{
									while (enumerator2.MoveNext())
									{
										VisualState visualState = (VisualState)enumerator2.get_Current();
										if (visualState != null)
										{
											Storyboard storyboard = visualState.get_Storyboard();
											if (storyboard != null)
											{
												using (IEnumerator<Timeline> enumerator3 = storyboard.get_Children().GetEnumerator())
												{
													while (enumerator3.MoveNext())
													{
														Timeline current = enumerator3.get_Current();
														DoubleAnimation doubleAnimation = current as DoubleAnimation;
														DoubleAnimationUsingKeyFrames doubleAnimationUsingKeyFrames = current as DoubleAnimationUsingKeyFrames;
														if (doubleAnimation != null)
														{
															this.ProcessDoubleAnimation(doubleAnimation);
														}
														else
														{
															if (doubleAnimationUsingKeyFrames != null)
															{
																this.ProcessDoubleAnimationWithKeys(doubleAnimationUsingKeyFrames);
															}
														}
													}
												}
											}
										}
									}
								}
								finally
								{
									IDisposable disposable = enumerator2 as IDisposable;
									if (disposable != null)
									{
										disposable.Dispose();
									}
								}
							}
						}
					}
					finally
					{
						IDisposable disposable2 = enumerator as IDisposable;
						if (disposable2 != null)
						{
							disposable2.Dispose();
						}
					}
				}
				this.UpdateKnownAnimations();
			}
		}
		private void UpdateKnownAnimations()
		{
			using (List<RelativeAnimatingContentControl.AnimationValueAdapter>.Enumerator enumerator = this._specialAnimations.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					RelativeAnimatingContentControl.AnimationValueAdapter current = enumerator.get_Current();
					current.UpdateWithNewDimension(this._knownWidth, this._knownHeight);
				}
			}
		}
		private void ProcessDoubleAnimationWithKeys(DoubleAnimationUsingKeyFrames da)
		{
			using (IEnumerator<DoubleKeyFrame> enumerator = da.get_KeyFrames().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					DoubleKeyFrame current = enumerator.get_Current();
					RelativeAnimatingContentControl.DoubleAnimationDimension? dimensionFromMagicNumber = RelativeAnimatingContentControl.GeneralAnimationValueAdapter<DoubleKeyFrame>.GetDimensionFromMagicNumber(current.get_Value());
					if (dimensionFromMagicNumber.get_HasValue())
					{
						this._specialAnimations.Add(new RelativeAnimatingContentControl.DoubleAnimationFrameAdapter(dimensionFromMagicNumber.get_Value(), current));
					}
				}
			}
		}
		private void ProcessDoubleAnimation(DoubleAnimation da)
		{
			if (da.get_To().get_HasValue())
			{
				RelativeAnimatingContentControl.DoubleAnimationDimension? dimensionFromMagicNumber = RelativeAnimatingContentControl.GeneralAnimationValueAdapter<DoubleAnimation>.GetDimensionFromMagicNumber(da.get_To().get_Value());
				if (dimensionFromMagicNumber.get_HasValue())
				{
					this._specialAnimations.Add(new RelativeAnimatingContentControl.DoubleAnimationToAdapter(dimensionFromMagicNumber.get_Value(), da));
				}
			}
			if (da.get_From().get_HasValue())
			{
				RelativeAnimatingContentControl.DoubleAnimationDimension? dimensionFromMagicNumber2 = RelativeAnimatingContentControl.GeneralAnimationValueAdapter<DoubleAnimation>.GetDimensionFromMagicNumber(da.get_To().get_Value());
				if (dimensionFromMagicNumber2.get_HasValue())
				{
					this._specialAnimations.Add(new RelativeAnimatingContentControl.DoubleAnimationFromAdapter(dimensionFromMagicNumber2.get_Value(), da));
				}
			}
		}
	}
}
